package com.ayuer.chapter10;

public class Fibonacci2Demo {
    public static void main(String[] args) {
        System.out.println(fibonacci(13));
    }


    /**
     * 使用动态规划，降维处理
     *
     * @param n
     * @return
     */
    public static int fibonacci(int n) {
        if (n == 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }

        /**
         * 类似滚动数组
         */
        int a = 0, b = 1;
        for (int i = 2; i <= n; i++) {
            int c = a + b;
            a = b;
            b = c;
        }
        return b;
    }
}
